package com.jubotech.business.web.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jubotech.business.web.dao.CustomerDao;
import com.jubotech.business.web.dao.WxAccountDao;
import com.jubotech.business.web.domain.WxAccountInfo;
import com.jubotech.business.web.query.DeviceQuery;
import com.jubotech.framework.common.ResultInfo;
import com.jubotech.framework.common.ServiceException;
import com.jubotech.framework.util.DateUtil;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional // 支持事务
public class WxAccountService {

	@Autowired
	private WxAccountDao weChatAccountDao;
	
	@Autowired
	private CustomerDao customerDao;
	 

	public WxAccountInfo getWeChatAccountByid(Integer id) {
		WxAccountInfo user = weChatAccountDao.findWeChatAccountInfoByid(id);
		return user;
	}
	public WxAccountInfo findWeChatAccountInfoByDeviceid(String deviceid) {
		WxAccountInfo user = weChatAccountDao.findWeChatAccountInfoByDeviceid(deviceid);
		return user;
	}
	
	public WxAccountInfo findWeChatAccountInfoByWeChatId(String weChatId) {
		WxAccountInfo user = weChatAccountDao.findWeChatAccountInfoByWeChatId(weChatId);
		return user;
	}
  
	public List<WxAccountInfo> findWeChatAccountInfoByWeChatIdNotEqualsDeviceid(String wechatid,String deviceid){
		return weChatAccountDao.findWeChatAccountInfoByWeChatIdNotEqualsDeviceid(wechatid, deviceid);
	}
	
	public List<WxAccountInfo> findWeChatAccountInfoByCid(Integer cid){
		return weChatAccountDao.findWeChatAccountInfoByCid(cid);
	}
	
	public void delete(Integer id) {
		if(null != id) {
			weChatAccountDao.deleteByPrimaryKey(id);
		}
	}

	public void update(WxAccountInfo user) {
		weChatAccountDao.update(user);
	}
 
	public void updateOnline(WxAccountInfo user) {
		weChatAccountDao.updateOnline(user);
	}
 
	public PageInfo<WxAccountInfo> pageList(DeviceQuery query) {
        PageHelper.startPage(query.getPage(), query.getRows());
       
        return new PageInfo<>(weChatAccountDao.pageList(query));
    }
	
	
	public List<WxAccountInfo> findAllDevice(DeviceQuery query) {
		return weChatAccountDao.pageList(query);
	}
	
	public int deleteByIds(String ids) {
		if (StringUtils.isBlank(ids)) {
			throw new ServiceException("invalid param");
		}
		String[] idArray = StringUtils.split(ids, ",");
		Set<String> userIds = Arrays.stream(idArray).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
		Example example = new Example(WxAccountInfo.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andIn("id", userIds);
 
		return weChatAccountDao.deleteByExample(example);
	}
 
	  
	 
	public ResultInfo insert(WxAccountInfo info) {
		ResultInfo res = new ResultInfo();
		Integer count = customerDao.getCustomerPhoneCount(info.getCid());//该客户端的的设备数
		Integer pcount =  weChatAccountDao.getPhoneAllCountByCid(info.getCid());//已经绑定的设备数
		if(pcount >= count){
			return ResultInfo.fail("超过账号个数");
		}else{
			try {
				String deviceid = info.getDeviceid().trim();
				info.setDeviceid(deviceid);
				info.setExpireTime(DateUtil.convertDate2String(DateUtil.offsetDay(new Date(), 5)));
				weChatAccountDao.insert(info);
			} catch (Exception e) {
				e.printStackTrace();
				return ResultInfo.fail("添加失败,请检查设备号是否重复");
			}
		}
		return res;
	}

	public ResultInfo updateDevice(WxAccountInfo info) {
		ResultInfo res = new ResultInfo();
		try {
		  
			WxAccountInfo newInfo = weChatAccountDao.findWeChatAccountInfoByid(info.getId());
			newInfo.setDeviceid(info.getDeviceid().trim());
			newInfo.setDevnickname(info.getDevnickname());
			newInfo.setBrand(info.getBrand());
			newInfo.setModule(info.getModule());

			weChatAccountDao.update(newInfo);
			
		} catch (Exception e) {
			return ResultInfo.fail("修改失败");
		}
		return res;
	}
 
	public void updateLoginTime(String deviceid) {
		weChatAccountDao.updateLoginTime(deviceid);
	}
}
